package cn.iocoder.yudao.module.lifefly.job;

import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
import cn.iocoder.yudao.framework.common.util.string.StrUtils;
import cn.iocoder.yudao.framework.quartz.core.handler.JobHandler;
import cn.iocoder.yudao.module.system.api.sms.SmsSendApi;
import cn.iocoder.yudao.module.system.api.sms.dto.send.SmsSendSingleToUserReqDTO;
import lombok.extern.slf4j.Slf4j;
import okhttp3.*;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
@Slf4j
public class SSXCheckInJob implements JobHandler {

    @Resource
    private SmsSendApi smsSendApi;

    @Override
    public String execute(String param) {
        Map<String, String> taskConfig = new HashMap();
        if (StrUtil.isNotEmpty(param)) {
            taskConfig = JsonUtils.parseMap(param);
        }
        HashMap<String, List<Cookie>> cookieStore = new HashMap<>();
        String finalRemark;
        String logincUrl = taskConfig.get("logincUrl") == null ? "https://6yhy.ml/auth/login" : taskConfig.get("logincUrl");
        String checkInUrl = taskConfig.get("checkInUrl") == null ? "https://6yhy.ml/user/checkin" : taskConfig.get("checkInUrl");
        String userAccount = taskConfig.get("userAccount") == null ? "liu1989fei2010@gmail.com" : taskConfig.get("userAccount");
        String userPasswd = taskConfig.get("userPasswd") == null ? "dhd478ksn" : taskConfig.get("userPasswd");
        OkHttpClient client = new OkHttpClient().newBuilder().cookieJar(new CookieJar() {
            @Override
            public void saveFromResponse(HttpUrl url, List<Cookie> cookies) {
                cookieStore.put(url.host(), cookies);
            }

            @Override
            public List<Cookie> loadForRequest(HttpUrl url) {
                List<Cookie> cookies = cookieStore.get(url.host());
                return cookies != null ? cookies : new ArrayList<Cookie>();
            }
        }).build();

        try {
            FormBody fb = new FormBody.Builder().add("email", userAccount).add("passwd", userPasswd).add("code", "").build();
            Request loginReq = new Request.Builder().post(fb).url(logincUrl).build();
            Response loginRsp = client.newCall(loginReq).execute();
            log.info("SSXChickInBean 登录请求响应:{}", StrUtils.unicode2String(loginRsp.body().string()));
            Request checkInReq = new Request.Builder().post(new FormBody.Builder().build()).url(checkInUrl).build();
            Response checkInRsp = client.newCall(checkInReq).execute();
            String result = StrUtils.unicode2String(checkInRsp.body().string());
            log.info("SSXChickInBean 签到请求响应:{}", result);
            finalRemark = result;
        } catch (Exception e) {
            finalRemark = e.getMessage();
            SmsSendSingleToUserReqDTO reqDTO = new SmsSendSingleToUserReqDTO();
            reqDTO.setUserId(100001L);
            reqDTO.setMobile("516038628@qq.com");
            reqDTO.setTemplateCode("EMAIL-ALL");
            HashMap<String, Object> map = new HashMap<>();
            map.put("ALL","SSXChickInBean 每日签到异常"+e.toString());
            reqDTO.setTemplateParams(map);
            smsSendApi.sendSingleSmsToMember(reqDTO);
            log.error("SSXChickInBean 签到任务异常", e);
        }
        return finalRemark;
    }
}
